
var OrderListView = new function() {
    var root = this;
    this.IsRendered = false;
    
    this.View = "#orders_content";    
    
    var htmlOrderListTemplate = GetTemplateByName('OrderList');
    var htmlOrderTemplate = GetTemplateByName('Order');
    var htmlOrderDetailsTemplate = GetTemplateByName('OrderDetails');
    var htmlOrderProductDetailsTemplate = GetTemplateByName('OrderProductDetails');

    this.RenderView = function() {
	
        if (!this.IsRendered) {
            air.trace("OrderListView: Rendering Order View");
            
            // Status tonen
            SetStatus("loading");
            
            // Alle orders overlopen in de lijst
            // en dat allemaal renderen naar html
            orders_html = '';
            for (var order_ref in OrderService.Orders) {
                orders_html += this.RenderOrderDetailsView(OrderService.Orders[order_ref]);
            }
            
            orders_html = htmlOrderListTemplate.replace(/{ORDERS_HOOK}/g, orders_html);
                    
            // HTML instellen
            $(this.View).html(orders_html);
            
            // Accordion activeren
            $("#order_list").accordion({
                autoHeight: false,
                collapsible: true
            });
            
            $("#order_list").accordion( "activate" , false);
            
            this.IsRendered = true;
            
            // Status resetten
            SetStatus("none");
        }
    }
    
    
    // Renders the details view for a given order
    this.RenderOrderDetailsView = function (order) {
        var order_html = htmlOrderTemplate;
        
        order_html = order_html.replace(/{ID_HOOK}/g, AddPrePendingZeros(order.ID, 6));
		
        order_html = order_html.replace(/{STATUS_HOOK}/g, order.State().Name);
		order_html = order_html.replace(/{STATUS_COLOR_HOOK}/g, colorscale(order.State().Color.replace("#", ""), .1));
		order_html = order_html.replace(/{ORDER_STATUS_IMAGE_HOOK}/g, order.State().StatusImage());
		
		
        order_html = order_html.replace(/{ORDER_DATE_HOOK}/g, order.OrderDate);
        order_html = order_html.replace(/{PAYMENT_GATEWAY_HOOK}/g, order.Payment);
        order_html = order_html.replace(/{PAYMENT_GATEWAY_IMG_HOOK}/g, this.GetPaymentGatewayImage(order.Payment));
        order_html = order_html.replace(/{PAYMENT_GATEWAY_IMG16_HOOK}/g, this.GetPaymentGatewayImage(order.Payment, 16));
        order_html = order_html.replace(/{PAYMENT_GATEWAY_IMG24_HOOK}/g, this.GetPaymentGatewayImage(order.Payment, 24));
        order_html = order_html.replace(/{PRODUCTS_AMOUNT_HOOK}/g, order.TotalProducts);
        order_html = order_html.replace(/{SHIPPING_AMOUNT_HOOK}/g, order.TotalShipping);
        order_html = order_html.replace(/{ORDER_AMOUNT_HOOK}/g, order.TotalAmount);
		order_html = order_html.replace(/{DISCOUNT_AMOUNT_HOOK}/g, order.TotalDiscount);
        
        // Customer velden
        order_html = order_html.replace(/{CUSTOMER_NAME_HOOK}/g, order.GetCustomer().Name());
        order_html = order_html.replace(/{CUSTOMER_ID_HOOK}/g, order.CustomerID);
        
        
        // Shopping Cart
        var order_products_html = '';
        // Then there where multiple products in cart
        for (var product_ref in order.Products) {
            order_products_html += this.RenderOrderProductDetailsView(order.Products[product_ref]);
        }
        order_html = order_html.replace(/{ORDER_DETAILS_HOOK}/g, order_products_html);
        
        return order_html;
    }
    
    // Renders the product details view for a given order
    this.RenderOrderProductDetailsView = function(product) {
        var cart_html = htmlOrderProductDetailsTemplate;
        
        cart_html = cart_html.replace(/{PRODUCT_ID_HOOK}/g, product.ID);
        cart_html = cart_html.replace(/{PRODUCT_NAME_HOOK}/g, product.Name);
        cart_html = cart_html.replace(/{PRODUCT_QUANTITY_HOOK}/g, product.Quantity);
        cart_html = cart_html.replace(/{PRODUCT_UNIT_PRICE_HOOK}/g, product.UnitPrice);
        
        return cart_html
    }
    
    this.GetPaymentGatewayImage = function (payment, size) {
        size = size == undefined ? '' : size;
        
        switch (payment) {
            case "Overschrijving":
                return "images/payment/payment_bank_wire" + size + ".png";
                break;
                
            case "PayPal":
                return "images/payment/payment_paypal" + size + ".png";
                break;
            
            case "cheque":
                return "images/payment/payment_cheque" + size + ".png";
                break;
                
            default:
                return "images/payment/payment_cash" + size + ".png";
                break;
        }
    }
    
}